﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TruyenCuoiViet.Source.Helper;
using com.vserv.windows.ads.wp8;
using com.vserv.windows.ads;

namespace TruyenCuoiViet.Source.UI
{
    public partial class AdPage : PhoneApplicationPage
    {
        public AdPage()
        {
            InitializeComponent();
        }
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            VservAdView adView = new VservAdView();
            adView.UX = VservAdUX.Interstitial; // To specify explicitly that you want to display Interstitial ads 
            adView.ZoneId = "d0b1df6e"; // This will set the zone from where ads will be fetched. 
            adView.TimeOut = 20; // To set timeout if Ad failed to fetch. 
            adView.Refresh = false;
            //adView.DidLoadAd += AdReceived;
            //adView.DidCacheAd += AdCached;
            //adView.DidInteractWithAd += InteractedWithAd;
            //adView.FailedToCacheAd += FailedToCacheAd;
            adView.FailedToLoadAd += VACCallback_OnVservAdClosing;
            adView.WillDismissOverlay += VACCallback_OnVservAdClosing;
            //grdAdd.Children.Add(adView);
            adView.LoadAd();
            await SaveLog.SaveLogInfo();
        }
        private void VACCallback_OnVservAdClosing(object sender, EventArgs e)
        {
            if (NavigationContext.QueryString.ContainsKey("Exit"))
            {
                Application.Current.Terminate();
            }
            else
            {
                NavigationService.Navigate(new Uri("/Source/Ui/MainPage.xaml", UriKind.Relative));
            }
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            VACCallback_OnVservAdClosing(null, null);
        }
        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            PhoneApplicationPage phoneApplicationPage = (PhoneApplicationPage)(((PhoneApplicationFrame)Application.Current.RootVisual)).Content;
            TransitionHelper.excuteTransition(phoneApplicationPage);
            base.OnNavigatingFrom(e);
        }
    }
}